package com.quectel.core.module.group.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quectel.core.module.device.dto.DeviceCategoryDto;
import com.quectel.core.module.device.service.DeviceCategoryService;
import com.quectel.core.module.group.dao.SysGroupDeviceCategoryDao;
import com.quectel.core.module.group.dto.SysGroupDeviceCategoryDto;
import com.quectel.core.module.group.entity.SysGroupDeviceCategoryEntity;
import com.quectel.core.module.group.service.SysGroupDeviceCategoryService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.Snowflake;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-05-31 12:13:44
 */
@DubboService
public class SysGroupDeviceCategoryServiceImpl implements SysGroupDeviceCategoryService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysGroupDeviceCategoryServiceImpl.class);

    @Autowired
    private SysGroupDeviceCategoryDao sysGroupDeviceCategoryDao;
    @Autowired
    private DeviceCategoryService deviceCategoryService;


    @Override
    public void deleteByGroupId(Long groupId) {
        sysGroupDeviceCategoryDao.delete(new QueryWrapper<SysGroupDeviceCategoryEntity>().eq("sys_group_id", groupId));
    }

    @Override
    public List<DeviceCategoryDto> selectByGroupId(Long groupId) {
        List<DeviceCategoryDto> result = new ArrayList<>();

        List<SysGroupDeviceCategoryEntity> groupDeviceCategoryEntityList = sysGroupDeviceCategoryDao.selectList(new QueryWrapper<SysGroupDeviceCategoryEntity>().eq("sys_group_id", groupId));
        if (groupDeviceCategoryEntityList != null && groupDeviceCategoryEntityList.size() > 0) {
            for (SysGroupDeviceCategoryEntity entity : groupDeviceCategoryEntityList) {
                DeviceCategoryDto deviceCategoryDto = deviceCategoryService.selectCacheById(entity.getDeviceCategoryId());
                if (deviceCategoryDto != null) {
                    result.add(deviceCategoryDto);
                }
            }
        }
        return result;
    }

    @Override
    public List<SysGroupDeviceCategoryDto> queryList(Map<String, Object> params) {
        List<SysGroupDeviceCategoryEntity> list = sysGroupDeviceCategoryDao.queryList(params);
        return CopyUtils.copyList(list, SysGroupDeviceCategoryDto.class);
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return sysGroupDeviceCategoryDao.queryTotal(params);
    }

    @Override
    public Long save(SysGroupDeviceCategoryDto dto) {
        SysGroupDeviceCategoryEntity entity = CopyUtils.copyObj(dto, SysGroupDeviceCategoryEntity.class);
        entity.setId(Snowflake.nextId());
        sysGroupDeviceCategoryDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SysGroupDeviceCategoryDto dto) {
        SysGroupDeviceCategoryEntity entity = CopyUtils.copyObj(dto, SysGroupDeviceCategoryEntity.class);
        sysGroupDeviceCategoryDao.updateById(entity);
    }

    @Override
    public SysGroupDeviceCategoryDto selectById(Long id) {
        SysGroupDeviceCategoryEntity entity = sysGroupDeviceCategoryDao.selectById(id);
        return CopyUtils.copyObj(entity, SysGroupDeviceCategoryDto.class);
    }

    @Override
    public void deleteById(Long id) {
        sysGroupDeviceCategoryDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
